package com.atguigu.spzx.controller;

import com.atguigu.spzx.model.dto.system.LoginDto;
import com.atguigu.spzx.model.dto.system.SysRoleDto;
import com.atguigu.spzx.model.dto.system.SysUserDto;
import com.atguigu.spzx.model.entity.system.SysUser;
import com.atguigu.spzx.model.vo.common.Result;
import com.atguigu.spzx.model.vo.common.ResultCodeEnum;
import com.atguigu.spzx.model.vo.system.LoginVo;
import com.atguigu.spzx.model.vo.system.SysMenuVo;
import com.atguigu.spzx.model.vo.system.ValidateCodeVo;
import com.atguigu.spzx.service.SysMenuService;
import com.atguigu.spzx.service.SysUserService;
import com.atguigu.spzx.service.ValidateCodeService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Map;
import java.util.Random;

@RestController
@RequestMapping("/admin/system/index")
//第一种注解实现跨域
//@CrossOrigin(allowedHeaders = "*", allowCredentials = "true", originPatterns = "*" )
//第二种配置类实现跨域 implememts WebMvcConfigurer
@Tag(name = "登入")
public class IndexController {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private ValidateCodeService validateCodeService;

    @Autowired
    private SysMenuService sysMenuService;

    //summary概述
    @Operation(summary = "登入")
    @PostMapping("/login")
    public Result<LoginVo> login(@RequestBody LoginDto loginDto) {

        LoginVo loginVo = sysUserService.login(loginDto);
        return Result.build(loginVo, ResultCodeEnum.SUCCESS);

    }


    @Operation(summary = "等出")
    @GetMapping(value = "/logout")
    public Result logout(@RequestHeader(value = "token") String token) {
        sysUserService.logout(token);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }


    @Operation(summary = "获得用户信息")
    @GetMapping(value = "/getUserInfo")
    public Result<SysUser> getUserInfo(@RequestHeader(name = "token", required = true) String token) {
        Map<String, String> map = sysUserService.getUserInfo(token);
        return Result.build(map, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "注册")
    @PostMapping("/register")
    public Result<SysUserDto> register(@RequestBody SysUserDto userDto) {
        return null;
    }


    @Operation(summary = "验证码")
    @GetMapping(value = "/generateValidateCode")
    public Result<ValidateCodeVo> generateValidateCode() {

        ValidateCodeVo validateCodeVo = validateCodeService.generateValidateCode();

        return Result.build(validateCodeVo, ResultCodeEnum.SUCCESS);
    }


    @GetMapping(value = "/menus")
    @Operation(summary = "菜单vo")
    public Result menus() {
        List<SysMenuVo> list = sysMenuService.findUserMenuList();
        return Result.build(list, ResultCodeEnum.SUCCESS);
    }

}
